# coding:utf-8

from databaseHandler import db
from datetime import datetime
from .userModel import *
from .replyModel import *
from .nodeModel import *


# 帖子的数据库模型
class Article(db.Entity):
    title = Required(str)  # 帖子标题
    # 帖子作者,和User表的字段相关联,和User表的articles字段是多(Article)对一(articles)关系
    author = Required('User', reverse='articles')
    node = Required('Node')  # 帖子所属的节点
    contents = Required(str)  # 帖子的内容
    created_time = Required(datetime)  # 帖子的发布时间,在提交的时候由系统自动生成
    replys = Set('Reply')  # 帖子的回复,和Reply表是一(replys)对多(Reply)的关系
    clicks = Required(int)  # 帖子的点击次数
    favorite = Set('User', reverse='favoritedArticles')  # 收藏帖子的用户


# 新帖子

@db_session
def newArticle(self, title, author, node, contents):
    Article(title=title,
            author=author,
            node=node,
            contents=contents,
            created_time=datetime.now(),
            clicks=0
            )
    return True

# 编辑帖子


@db_session
def editArticle(articleId):
    article = Article.get(id=articleId)


# 从数据库完全删除帖子,不可逆

@db_session
def deleteArticleById(self, articleId):
    Article[articleId].delete()
    return True
